package cn.zh.base.controller;

import cn.zh.base.dto.EmailRegisterDto;
import cn.zh.base.dto.PhoneRegisterDto;
import cn.zh.base.service.VerifyCodeService;
import cn.zh.base.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private VerifyCodeService verifyCodeService;

    /**
     * 获取图形验证码
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String imageCodeKey) throws IOException {
        String base64ImageStr = verifyCodeService.imageCode(imageCodeKey);
        return AjaxResult.me().success(base64ImageStr);
    }

    /**
     * 发送短信验证码
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody PhoneRegisterDto dto) {
        verifyCodeService.smsCode(dto);
        return  AjaxResult.me().success();
    }

    /**
     * 发送邮箱验证码
     */
    @PostMapping ("/emailCode")
    public AjaxResult emailCode(@RequestBody EmailRegisterDto dto) throws IOException {
        verifyCodeService.emailCode(dto);
        return AjaxResult.me().success();
    }


    /**
     * 手机号注册接口
     */
    @PostMapping("/phoneRegister")
    public AjaxResult phoneRegister(@RequestBody PhoneRegisterDto dto) throws IOException {
        verifyCodeService.phoneRegister(dto);
        return AjaxResult.me().success();
    }

    /**
     * 邮箱注册接口
     */
    @PostMapping("/emailRegister")
    public AjaxResult email(@RequestBody EmailRegisterDto dto) throws IOException {
        verifyCodeService.emailRegister(dto);
        return AjaxResult.me().success();

    }
}
